<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>微博用户列表</title>
    <link rel="stylesheet" href="/MicroBlog/Public/Admin/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/MicroBlog/Public/Admin/css/global.css" media="all">
    <link rel="stylesheet" href="/MicroBlog/Public/Admin/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/MicroBlog/Public/Admin/css/table.css"/>
    <script type="text/javascript" src="/MicroBlog/Public/Admin/js/jquery-1.7.2.min.js"></script>
</head>
<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote" style="height: 30px;">
        <div class="layui-input-inline" style="float:right;">
            <div class="layui-input-inline">
                <input type="text" name="username" placeholder="用户昵称" autocomplete="off" class="layui-input">
            </div>
            <a href="#" class="layui-btn layui-btn-small" id="search">
                <i class="layui-icon">&#xe615;</i> 搜索
            </a>
        </div>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>微博用户列表</legend>
        <div class="layui-field-box">
            <table class="site-table table-hover">
                <thead>
                <tr>
                    <th style="text-align: center">ID
                    </th>
                    <th style="text-align: center">用户昵称
                    </th>
                    <th style="text-align: center">头像</th>
                    <th style="text-align: center">关注信息</th>
                    <th style="text-align: center">注册时间</th>
                    <th style="text-align: center">账号状态</th>
                    <th style="text-align: center">操作</th>
                </tr>
                </thead>
                <tbody id="content">
                </tbody>
            </table>
        </div>
    </fieldset>
    <div class="admin-table-page">
        <div id="paged" class="page">
        </div>
    </div>
</div>

<!--模板-->
<script type="text/html" id="tpl">
    <!--
        toDateString()：将时间戳转换成日期
        digit():数字前置加零
    -->
    {{#
    layui.laytpl.toDateString = function(d, format){
    var date = new Date(d || new Date())
    ,ymd = [
    this.digit(date.getFullYear(), 4)
    ,this.digit(date.getMonth() + 1)
    ,this.digit(date.getDate())
    ]
    ,hms = [
    this.digit(date.getHours())
    ,this.digit(date.getMinutes())
    ,this.digit(date.getSeconds())
    ];
    format = format || 'yyyy年MM月dd日 HH点mm分ss秒';
    return format.replace(/yyyy/g, ymd[0])
    .replace(/MM/g, ymd[1])
    .replace(/dd/g, ymd[2])
    .replace(/HH/g, hms[0])
    .replace(/mm/g, hms[1])
    .replace(/ss/g, hms[2]);
    };
    layui.laytpl.digit = function(num, length, end){
    var str = '';
    num = String(num);
    length = length || 2;
    for(var i = num.length; i < length; i++){
    str += '0';
    }
    return num < Math.pow(10, length) ? str + (num|0) : num;
    };
    }}
    {{#}}
    {{# layui.each(d.list, function(index, item){ }}
    <tr>
        <td>{{ item.id }}</td>
        <td>{{ item.username }}</td>
        <td>
            {{# if(item.face != ''){ }}
            <img src="/MicroBlog/{{ item.face }}" width='50' height='50'/>
            {{# } else { }}
            <img src="/MicroBlog/Public/Home/Images/noface.gif" width='50' height='50'/>
            {{# } }}
        </td>
        <td>
            <ul>
                <li>关注：{{ item.follow}}</li>
                <li>粉丝：{{ item.fans}}</li>
                <li>微博：{{ item.weibo}}</li>
            </ul>
        </td>
        <td>{{ layui.laytpl.toDateString(item.registime*1000) }}</td>
        <td>
            {{# if(item.lock == 1){ }}
                锁定
            {{# } else { }}
                正常
            {{# } }}

        </td>
        <td>
            <a href="javascript:;" data-name="{{ item.id }}" data-opt="retpass"
               class="layui-btn layui-btn-mini">重置密码</a>
            {{# if(item.lock == 1){ }}
            <a href="javascript:;" data-name="{{ item.id }}" data-value="{{ item.lock }}" data-opt="retstatus"
               class="layui-btn layui-btn-danger layui-btn-mini">解除锁定</a>
            {{# } else { }}
            <a href="javascript:;" data-name="{{ item.id }}" data-value="{{ item.lock }}" data-opt="retstatus"
               class="layui-btn layui-btn-danger layui-btn-mini">锁定用户</a>
            {{# } }}
        </td>
    </tr>
    {{# }); }}
</script>

<script type="text/javascript" src="/MicroBlog/Public/Admin/plugins/layui/layui.js"></script>
<script>
    $(function () {
        getUserList('');
    });

    function getUserList(username) {
        layui.config({
            base: '/MicroBlog/Public/Admin/js/'
        });
        layui.use(['paging', 'form'], function () {
            var $ = layui.jquery,
                paging = layui.paging(),
                layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
                layer = layui.layer, //获取当前窗口的layer对象
                form = layui.form();

            paging.init({
                url: "<?php echo U('User/getUserListByPage','','');?>", //地址
                elem: '#content', //内容容器
                params: { //发送到服务端的参数
                    username: username
                },
                type: 'post',
                tempElem: '#tpl', //模块容器
                pageConfig: { //分页参数配置
                    elem: '#paged', //分页容器
                    pageSize: "<?php echo (C("pageSize")); ?>" //使用paging.js配置的默认值15
                },
                success: function () { //渲染成功的回调
                },
                fail: function (msg) { //获取数据失败的回调
                },
                complate: function () { //完成的回调
                    //绑定按钮事件
                    $('#content').children('tr').each(function (data) {
                        var $that = $(this);
                        //重置密码
                        $that.children('td:last-child').children('a[data-opt=retpass]').on('click', function () {
                            $.ajax({
                                url: "<?php echo U('User/resetPwd','','');?>",
                                type: "post",
                                dataType: "json",
                                data: 'id=' + $(this).data('name'),
                                success: function (result) {
                                    layer.msg(result.msg, {time: 800});
                                }, error: function () {
                                }
                            });
                        });
                        //修改用户状态
                        $that.children('td:last-child').children('a[data-opt=retstatus]').on('click', function () {
                            $.ajax({
                                url: "<?php echo U('User/editUserStatus','','');?>",
                                type: "post",
                                dataType: "json",
                                data: 'id=' + $(this).data('name') + '&status=' + $(this).data('value'),
                                success: function (result) {
                                    layer.msg(result.msg, {time: 800}, function () {
                                        //操作完成后通过js直接修改当前表格行的数据值（不建议重新刷新整个页面）
                                        if (result.errorCode == 0){
                                            $that.children('td:last-child').children('a[data-opt=retstatus]').data('value', result.lock);
                                            $lockObj = $(window.parent.frames['iframe'].contentDocument).find('span[id=lock]');
                                            $lockCount = $lockObj.text();
                                            if(result.lock == 1){
                                                $that.children('td:last-child').prev().text('锁定');
                                                $that.children('td:last-child').children('a[data-opt=retstatus]').text('解除锁定');
                                                $lockObj.text(parseInt($lockCount)+1);
                                            }else{
                                                $that.children('td:last-child').prev().text('正常');
                                                $that.children('td:last-child').children('a[data-opt=retstatus]').text('锁定用户');
                                                $lockObj.text(parseInt($lockCount)-1);
                                            }
                                        }
                                    });
                                }, error: function () {
                                }
                            });
                        });
                    });
                },
            });
        });
    }

    $('#search').on('click', function () {
        getUserList($.trim($('input[name=username]').val()));
    });
</script>
</body>

</html>